﻿Imports System
Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices

Namespace System.Workflow.Interop
    Friend Class NativeMethods
        ' Methods
        Shared Sub New()
            If (Marshal.SystemDefaultCharSize = 1) Then
                NativeMethods.TTN_GETDISPINFO = -520
                NativeMethods.TTN_NEEDTEXT = -520
                NativeMethods.TTM_ADDTOOL = &H404
                NativeMethods.TTM_SETTITLE = &H420
                NativeMethods.TTM_DELTOOL = &H405
                NativeMethods.TTM_NEWTOOLRECT = &H406
                NativeMethods.TTM_GETTOOLINFO = &H408
                NativeMethods.TTM_SETTOOLINFO = &H409
                NativeMethods.TTM_HITTEST = &H40A
                NativeMethods.TTM_GETTEXT = &H40B
                NativeMethods.TTM_UPDATETIPTEXT = &H40C
                NativeMethods.TTM_ENUMTOOLS = &H40E
                NativeMethods.TTM_GETCURRENTTOOL = &H40F
            Else
                NativeMethods.TTN_GETDISPINFO = -530
                NativeMethods.TTN_NEEDTEXT = -530
                NativeMethods.TTM_ADDTOOL = &H432
                NativeMethods.TTM_SETTITLE = &H421
                NativeMethods.TTM_DELTOOL = &H433
                NativeMethods.TTM_NEWTOOLRECT = &H434
                NativeMethods.TTM_GETTOOLINFO = &H435
                NativeMethods.TTM_SETTOOLINFO = &H436
                NativeMethods.TTM_HITTEST = &H437
                NativeMethods.TTM_GETTEXT = &H438
                NativeMethods.TTM_UPDATETIPTEXT = &H439
                NativeMethods.TTM_ENUMTOOLS = &H43A
                NativeMethods.TTM_GETCURRENTTOOL = &H43B
            End If
        End Sub

        <DllImport("gdi32", CharSet:=CharSet.Auto)> _
        Friend Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function DeleteObject(ByVal hObject As HandleRef) As Integer
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function ExtCreatePen(ByVal style As Integer, ByVal nWidth As Integer, ByVal logbrush As LOGBRUSH, ByVal styleArrayLength As Integer, ByVal styleArray As Integer()) As IntPtr
        End Function

        Friend Shared Function Failed(ByVal hr As Integer) As Boolean
            Return (hr < 0)
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function GetCurrentObject(ByVal hDC As HandleRef, ByVal uObjectType As UInt32) As IntPtr
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
        Public Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal nIndex As Integer) As Integer
        End Function

        Friend Shared Function Header_GetItem(ByVal hWndHeader As IntPtr, ByVal index As Integer, <[In], Out> ByVal hdi As HDITEM) As Boolean
            If Not (NativeMethods.SendMessage(hWndHeader, &H120B, New IntPtr(index), hdi) <> IntPtr.Zero) Then
                Return False
            End If
            Return True
        End Function

        Friend Shared Function Header_SetItem(ByVal hWndHeader As IntPtr, ByVal index As Integer, <[In], Out> ByVal hdi As HDITEM) As Boolean
            If Not (NativeMethods.SendMessage(hWndHeader, &H120C, New IntPtr(index), hdi) <> IntPtr.Zero) Then
                Return False
            End If
            Return True
        End Function

        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Public Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function LineTo(ByVal hdc As HandleRef, ByVal x As Integer, ByVal y As Integer) As Boolean
        End Function

        Friend Shared Function ListView_GetHeader(ByVal hWndLV As IntPtr) As IntPtr
            Return NativeMethods.SendMessage(hWndLV, &H101F, IntPtr.Zero, IntPtr.Zero)
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function MoveToEx(ByVal hdc As HandleRef, ByVal x As Integer, ByVal y As Integer, ByVal pt As POINT) As Boolean
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function SelectObject(ByVal hdc As HandleRef, ByVal obj As HandleRef) As IntPtr
        End Function

        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function

        <DllImport("user32", CharSet:=CharSet.Auto)> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, <[In], Out> ByVal lParam As HDITEM) As IntPtr
        End Function

        <DllImport("user32.dll")> _
        Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef rc As RECT) As IntPtr
        End Function

        <DllImport("user32.dll")> _
        Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef ti As TOOLINFO) As IntPtr
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function SetGraphicsMode(ByVal hdc As HandleRef, ByVal iMode As Integer) As Integer
        End Function

        <DllImport("user32.dll")> _
        Friend Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hwndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal flags As Integer) As Integer
        End Function

        <DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function SetWorldTransform(ByVal hdc As HandleRef, ByVal xform As XFORM) As Integer
        End Function

        Friend Shared Function ThrowOnFailure(ByVal hr As Integer) As Integer
            Return NativeMethods.ThrowOnFailure(hr, Nothing)
        End Function

        Friend Shared Function ThrowOnFailure(ByVal hr As Integer, ByVal ParamArray expectedHRFailure As Integer()) As Integer
            If (NativeMethods.Failed(hr) AndAlso ((expectedHRFailure Is Nothing) OrElse (Array.IndexOf([Of] Integer)(expectedHRFailure, hr) < 0))) Then
                Marshal.ThrowExceptionForHR(hr)
            End If
            Return hr
        End Function


        ' Fields
        Friend Const BS_SOLID As Integer = 0
        Friend Const GM_ADVANCED As Integer = 2
        Friend Const HDF_BITMAP As Integer = &H2000
        Friend Const HDF_BITMAP_ON_RIGHT As Integer = &H1000
        Friend Const HDF_CENTER As Integer = 2
        Friend Const HDF_IMAGE As Integer = &H800
        Friend Const HDF_JUSTIFYMASK As Integer = 3
        Friend Const HDF_LEFT As Integer = 0
        Friend Const HDF_OWNERDRAW As Integer = &H8000
        Friend Const HDF_RIGHT As Integer = 1
        Friend Const HDF_RTLREADING As Integer = 4
        Friend Const HDF_SORTDOWN As Integer = &H200
        Friend Const HDF_SORTUP As Integer = &H400
        Friend Const HDF_STRING As Integer = &H4000
        Friend Const HDI_BITMAP As Integer = &H10
        Friend Const HDI_DI_SETITEM As Integer = &H40
        Friend Const HDI_FILTER As Integer = &H100
        Friend Const HDI_FORMAT As Integer = 4
        Friend Const HDI_HEIGHT As Integer = 1
        Friend Const HDI_IMAGE As Integer = &H20
        Friend Const HDI_LPARAM As Integer = 8
        Friend Const HDI_ORDER As Integer = &H80
        Friend Const HDI_TEXT As Integer = 2
        Friend Const HDI_WIDTH As Integer = 1
        Friend Const HDM_GETITEM As Integer = &H120B
        Friend Const HDM_SETITEM As Integer = &H120C
        Friend Const HOLLOW_BRUSH As Integer = 5
        Friend Const HORZRES As Integer = 8
        Friend Const LARGE_ICON As Integer = 1
        Friend Const LOGPIXELSX As Integer = &H58
        Friend Const LOGPIXELSY As Integer = 90
        Friend Const LVM_GETHEADER As Integer = &H101F
        Friend Const OBJ_BRUSH As Integer = 2
        Friend Const OBJ_EXTPEN As Integer = 11
        Friend Const OBJ_PEN As Integer = 1
        Friend Const PHYSICALHEIGHT As Integer = &H6F
        Friend Const PHYSICALOFFSETX As Integer = &H70
        Friend Const PHYSICALOFFSETY As Integer = &H71
        Friend Const PHYSICALWIDTH As Integer = 110
        Friend Const PS_COSMETIC As Integer = 0
        Friend Const PS_SOLID As Integer = 0
        Friend Const PS_USERSTYLE As Integer = 7
        Friend Const SMALL_ICON As Integer = 0
        Friend Const SWP_NOACTIVATE As Integer = &H10
        Friend Const SWP_NOSIZE As Integer = 1
        Friend Const SWP_NOZORDER As Integer = 4
        Friend Const TTDT_AUTOMATIC As Integer = 0
        Friend Const TTDT_AUTOPOP As Integer = 2
        Friend Const TTDT_INITIAL As Integer = 3
        Friend Const TTDT_RESHOW As Integer = 1
        Friend Const TTF_ABSOLUTE As Integer = &H80
        Friend Const TTF_CENTERTIP As Integer = 2
        Friend Const TTF_DI_SETITEM As Integer = &H8000
        Friend Const TTF_IDISHWND As Integer = 1
        Friend Const TTF_PARSELINKS As Integer = &H1000
        Friend Const TTF_RTLREADING As Integer = 4
        Friend Const TTF_SUBCLASS As Integer = &H10
        Friend Const TTF_TRACK As Integer = &H20
        Friend Const TTF_TRANSPARENT As Integer = &H100
        Friend Const TTI_ERROR As Integer = 3
        Friend Const TTI_INFO As Integer = 1
        Friend Const TTI_NONE As Integer = 0
        Friend Const TTI_WARNING As Integer = 2
        Friend Shared ReadOnly TTM_ACTIVATE As Integer = &H401
        Friend Shared ReadOnly TTM_ADDTOOL As Integer
        Private Const TTM_ADDTOOLA As Integer = &H404
        Private Const TTM_ADDTOOLW As Integer = &H432
        Friend Shared ReadOnly TTM_ADJUSTRECT As Integer = &H41F
        Friend Shared ReadOnly TTM_DELTOOL As Integer
        Private Const TTM_DELTOOLA As Integer = &H405
        Private Const TTM_DELTOOLW As Integer = &H433
        Friend Shared ReadOnly TTM_ENUMTOOLS As Integer
        Private Const TTM_ENUMTOOLSA As Integer = &H40E
        Private Const TTM_ENUMTOOLSW As Integer = &H43A
        Friend Shared ReadOnly TTM_GETCURRENTTOOL As Integer
        Private Const TTM_GETCURRENTTOOLA As Integer = &H40F
        Private Const TTM_GETCURRENTTOOLW As Integer = &H43B
        Friend Shared ReadOnly TTM_GETDELAYTIME As Integer = &H415
        Friend Shared ReadOnly TTM_GETTEXT As Integer
        Private Const TTM_GETTEXTA As Integer = &H40B
        Private Const TTM_GETTEXTW As Integer = &H438
        Friend Shared ReadOnly TTM_GETTOOLINFO As Integer
        Private Const TTM_GETTOOLINFOA As Integer = &H408
        Private Const TTM_GETTOOLINFOW As Integer = &H435
        Friend Shared ReadOnly TTM_HITTEST As Integer
        Private Const TTM_HITTESTA As Integer = &H40A
        Private Const TTM_HITTESTW As Integer = &H437
        Friend Shared ReadOnly TTM_NEWTOOLRECT As Integer
        Private Const TTM_NEWTOOLRECTA As Integer = &H406
        Private Const TTM_NEWTOOLRECTW As Integer = &H434
        Friend Shared ReadOnly TTM_POP As Integer = &H41C
        Friend Shared ReadOnly TTM_RELAYEVENT As Integer = &H407
        Friend Shared ReadOnly TTM_SETDELAYTIME As Integer = &H403
        Friend Shared ReadOnly TTM_SETMAXTIPWIDTH As Integer = &H418
        Friend Shared ReadOnly TTM_SETTITLE As Integer
        Private Const TTM_SETTITLEA As Integer = &H420
        Private Const TTM_SETTITLEW As Integer = &H421
        Friend Shared ReadOnly TTM_SETTOOLINFO As Integer
        Private Const TTM_SETTOOLINFOA As Integer = &H409
        Private Const TTM_SETTOOLINFOW As Integer = &H436
        Friend Shared ReadOnly TTM_TRACKACTIVATE As Integer = &H411
        Friend Shared ReadOnly TTM_TRACKPOSITION As Integer = &H412
        Friend Shared ReadOnly TTM_UPDATE As Integer = &H41D
        Friend Shared ReadOnly TTM_UPDATETIPTEXT As Integer
        Private Const TTM_UPDATETIPTEXTA As Integer = &H40C
        Private Const TTM_UPDATETIPTEXTW As Integer = &H439
        Friend Shared ReadOnly TTM_WINDOWFROMPOINT As Integer = &H410
        Friend Shared ReadOnly TTN_GETDISPINFO As Integer
        Private Const TTN_GETDISPINFOA As Integer = -520
        Private Const TTN_GETDISPINFOW As Integer = -530
        Friend Shared ReadOnly TTN_NEEDTEXT As Integer
        Private Const TTN_NEEDTEXTA As Integer = -520
        Private Const TTN_NEEDTEXTW As Integer = -530
        Friend Shared ReadOnly TTN_POP As Integer = -522
        Friend Shared ReadOnly TTN_SHOW As Integer = -521
        Friend Const TTS_ALWAYSTIP As Integer = 1
        Friend Const TTS_BALLOON As Integer = &H40
        Friend Const TTS_CLOSE As Integer = &H80
        Friend Const TTS_NOANIMATE As Integer = &H10
        Friend Const TTS_NOFADE As Integer = &H20
        Friend Const TTS_NOPREFIX As Integer = 2
        Friend Const VERTRES As Integer = 10
        Friend Const WM_KEYDOWN As Integer = &H100
        Friend Const WM_KEYUP As Integer = &H101
        Friend Const WM_NOTIFY As Integer = &H4E
        Friend Const WM_SETFONT As Integer = &H30
        Friend Const WM_SETICON As Integer = &H80
        Friend Const WM_SETREDRAW As Integer = 11
        Friend Const WM_SYSKEYDOWN As Integer = 260
        Friend Const WM_SYSKEYUP As Integer = &H105
        Friend Const WS_EX_DLGMODALFRAME As Integer = 1
        Friend Const WS_EX_TOPMOST As Integer = 8
        Friend Const WS_POPUP As Integer = -2147483648

        ' Nested Types
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto), ComVisible(False)> _
        Friend Class HDITEM
            Public mask As Integer
            Public cxy As Integer
            Public pszText As IntPtr = IntPtr.Zero
            Public hbm As IntPtr = IntPtr.Zero
            Public cchTextMax As Integer
            Public fmt As Integer
            Public lParam As Integer
            Public image As Integer
            Public order As Integer
            Public type As Integer
            Public filter As IntPtr = IntPtr.Zero
        End Class

        <StructLayout(LayoutKind.Sequential)> _
        Friend Class LOGBRUSH
            Public lbStyle As Integer
            Public lbColor As Integer
            Public lbHatch As Long
            Public Sub New(ByVal style As Integer, ByVal color As Integer, ByVal hatch As Integer)
                Me.lbStyle = style
                Me.lbColor = color
                Me.lbHatch = hatch
            End Sub
        End Class

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
        Friend Class NMHDR
            Public hwndFrom As IntPtr = IntPtr.Zero
            Public idFrom As Integer = 0
            Public code As Integer = 0
        End Class

        <StructLayout(LayoutKind.Sequential)> _
        Friend Class POINT
            Public x As Integer
            Public y As Integer
        End Class

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
        Friend Structure RECT
            Public left As Integer
            Public top As Integer
            Public right As Integer
            Public bottom As Integer
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
        Friend Structure TOOLINFO
            Public size As Integer
            Public flags As Integer
            Public hwnd As IntPtr
            Public id As IntPtr
            Public rect As RECT
            Public hinst As IntPtr
            Public [text] As IntPtr
            Public lParam As IntPtr
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Friend Class XFORM
            Public eM11 As Single
            Public eM12 As Single
            Public eM21 As Single
            Public eM22 As Single
            Public eDx As Single
            Public eDy As Single
            Public Sub New()
                Me.eM11 = 1!
                Me.eM22 = 1!
            End Sub

            Public Sub New(ByVal transform As Matrix)
                Me.eM11 = 1!
                Me.eM22 = 1!
                Me.eM11 = transform.Elements(0)
                Me.eM12 = transform.Elements(1)
                Me.eM21 = transform.Elements(2)
                Me.eM22 = transform.Elements(3)
                Me.eDx = transform.Elements(4)
                Me.eDy = transform.Elements(5)
            End Sub
        End Class
    End Class
End Namespace

